//
//  YWDetectAndUpkeepRecordTableViewCell.m
//  maintain
//
//  Created by Kyo on 16/9/27.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWDetectAndUpkeepRecordTableViewCell.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UILabel+AttributedText.h"

#import "YWEquipmentMaintenanceRecord.h"
#import "YWEquipment.h"
#import "YWEquipmentManager.h"
#import "YWBuildingManager.h"

@interface YWDetectAndUpkeepRecordTableViewCell()
/** 状态Label */
@property (weak, nonatomic) UILabel *codeLabel;
/** 报障人 */
@property (weak, nonatomic) UILabel *executorLabel;
/** 设备 */
@property (weak, nonatomic) UILabel *equipmentLabel;
/** 位置 */
@property (weak, nonatomic) UILabel *locationLabel;
/** 备注 */
@property (weak, nonatomic) UILabel *noteLabel;
@end

@implementation YWDetectAndUpkeepRecordTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setMaintenanceRecord:(YWEquipmentMaintenanceRecord *)maintenanceRecord{
    _maintenanceRecord = maintenanceRecord;
    [_codeLabel labelText:@"编号: " withAttributedText:maintenanceRecord.recordSequence?:@"无" andAttributedColor:[UIColor blackColor]];
    
    [_executorLabel labelText:@"执行人: " withAttributedText:maintenanceRecord.handlerName?:@"无" andAttributedColor:[UIColor customYellowColor]];
    
    YWEquipment *equipment = [[YWEquipmentManager shareManager] equipmentWithId:maintenanceRecord.equipmentId];
    [_equipmentLabel labelText:@"设备: " withAttributedText:equipment.equipmentName?:@"无" andAttributedColor:[UIColor blackColor]];
    
    NSString *location = [[YWBuildingManager shareManager] equipmentLocation:equipment];
    [_locationLabel labelText:@"位置: " withAttributedText:location.length?location:@"无" andAttributedColor:[UIColor blackColor]];
    
    [_noteLabel labelText:@"备注: " withAttributedText:maintenanceRecord.note.length?maintenanceRecord.note:@"无" andAttributedColor:[UIColor blackColor]];
    
    _locationLabel.preferredMaxLayoutWidth = _locationLabel.frame.size.width;
    _noteLabel.preferredMaxLayoutWidth = _noteLabel.frame.size.width;
}

- (void)setUpSubviews {
    
    UIView *superview = self.contentView;
    [self.codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superview.mas_top).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
    }];
    [self.executorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_codeLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
    }];
    [self.equipmentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_executorLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
    }];
    [self.locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_equipmentLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
    }];
    [self.noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_locationLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
        make.bottom.mas_equalTo(superview.mas_bottom).offset(-8);
    }];
}

-(UILabel *)codeLabel{
    if (!_codeLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        _codeLabel = label;
        [self.contentView addSubview:_codeLabel];
    }
    return _codeLabel;
}

-(UILabel *)executorLabel{
    if (!_executorLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        _executorLabel = label;
        [self.contentView addSubview:_executorLabel];
    }
    return _executorLabel;
}

- (UILabel *)equipmentLabel{
    if (!_equipmentLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        _equipmentLabel = label;
        [self.contentView addSubview:_equipmentLabel];
    }
    return _equipmentLabel;
}

- (UILabel *)locationLabel{
    if (!_locationLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.numberOfLines = 0;
        label.textColor = [UIColor customTextGrayBlackColor];
        _locationLabel = label;
        [self.contentView addSubview:_locationLabel];
    }
    return _locationLabel;
}

- (UILabel *)noteLabel{
    if (!_noteLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.numberOfLines = 0;
        label.textColor = [UIColor customTextGrayBlackColor];
        _noteLabel = label;
        [self.contentView addSubview:_noteLabel];
    }
    return _noteLabel;
}

@end
